﻿using System;
using System.IO;

namespace StudentInfoManager
{
    public class ClassRoom
    {
        public ClassRoom(int studentsCount)
        {
            Students = new Student[studentsCount];
            Count = 0;
        }

        public Student[] Students { get; set; }

        private int Count { get; set; }

        public bool IsFull 
        {
            get
            {
                return Count >= Students.Length;
            }
        }

        public bool Add(Student student)
        {
            if (!IsFull)
            {
                Students[Count] = student;
                Count++;
                return true;
            }
            else
            {
                return false;
            }
        }

        public string MailingList
        {
            get
            {
                string list = "";
                for (int i = 0; i < Count; i++)
                {
                    if (Students[i] != null)
                    {
                        list += Students[i].ToString();
                        list += Environment.NewLine;
                    }
                }

                return list;
            }
        }


        public static void Serialize(string path, string content)
        {
            File.WriteAllText(path, content);
        }
        public static Student[] Deserialize(string path)
        {
            if (!File.Exists(path))
            {
                return new Student[0];
            }
            
            string[] lines = File.ReadAllLines(path);
            
            Student[] students = new Student[lines.Length];

            for (int i = 0; i < students.Length; i++)
            {
                students[i] = Student.Parse(lines[i]);
            }

            return students;
        }
    }
}
